package com.sztx.pay.center.core.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.sztx.pay.center.common.yhbf.YHBFSensitiveEnc;
import com.sztx.pay.center.core.service.BankUtilService;
import com.sztx.pay.center.core.service.bean.BankInfo;
import com.sztx.se.common.exception.BusinessException;
import com.sztx.se.core.service.impl.BaseServiceImpl;
import com.sztx.se.rpc.dubbo.client.DubboClientFactory;
import com.sztx.trade.payment.gateway.rpc.api.service.bankutil.BankListRpcService;
import com.sztx.trade.payment.gateway.rpc.api.service.bankutil.BankUtilRpcService;
import com.sztx.trade.payment.gateway.rpc.api.service.bankutil.ReqGetBankCardInfo;
import com.sztx.trade.payment.gateway.rpc.api.service.bankutil.ReqGetSupportedBankList;
import com.sztx.trade.payment.gateway.rpc.api.service.bankutil.RespGetBankCardInfo;
import com.sztx.trade.payment.gateway.rpc.api.service.bankutil.RespGetBankCardInfo.BankCardInfo;
import com.sztx.trade.payment.gateway.rpc.api.service.bankutil.RespGetSupportedBankList;
import com.sztx.trade.payment.gateway.rpc.api.service.cardbinding.BankCardBindingRpcService;
import com.sztx.trade.payment.gateway.rpc.api.service.cardbinding.ReqQueryBindBankCardInfo;
import com.sztx.trade.payment.gateway.rpc.api.service.cardbinding.RespQueryBindBankCardInfo;
@Service
public class BankUtilServiceImpl extends BaseServiceImpl implements BankUtilService {
	
	@Autowired
	private DubboClientFactory dubboConsumerFactory;

	@Override
	public BankInfo queryBankInfo(String cardNo) {
		BankInfo bankInfo = null;
		cardNo = YHBFSensitiveEnc.passDatasensitiveEnc(cardNo,"cardno");
		BankUtilRpcService bankUtilRpcService = dubboConsumerFactory.getDubboClient("bankUtilRpcService");		
		ReqGetBankCardInfo request = new ReqGetBankCardInfo();
		request.setBankCardNo(cardNo);
		
		LOG.info("GetBankCardInfo Request:"+JSONObject.toJSONString(request));
		RespGetBankCardInfo resp = bankUtilRpcService.getBankCardInfo(request);
		LOG.info("GetBankCardInfo Response:"+JSONObject.toJSONString(resp));
		
		if (resp == null) {
			LOG.info("调用网关GetBankCardInfo无响应");
			throw new BusinessException("服务器忙请稍后再试");
		}
		
		String resultCode = resp.getResultCode();
		String resultMsg = resp.getResultMsg();
		
		if (!RespGetBankCardInfo.RC_OK.equals(resultCode)) {
			LOG.warn("调用网关GetBankCardInfo出错：resultCode="+resultCode+",resultMsg="+resultMsg);
//			throw new BusinessException(resultMsg);
			throw new BusinessException("没有查询到银行卡信息");
		}
			
		BankCardInfo cardInfo = resp.getCardInfo();
		bankInfo = new BankInfo();
		bankInfo.setBankName(cardInfo.getBankName());
		bankInfo.setBankType(cardInfo.getBankType());
		bankInfo.setSwiftCode(cardInfo.getSwiftCode());
		bankInfo.setCardType(cardInfo.getCardType());
		bankInfo.setPassClipherPay(cardInfo.isPassCipherPay());
		return bankInfo;
	}

	@Override
	public List<BankInfo> querySupportedBankList(int type) {
		
		List<BankInfo> list = null;
		
		BankListRpcService tradeBankListRpcService = dubboConsumerFactory.getDubboClient("bankListRpcService");		
//		Integer payType = type.getValue();
		
		ReqGetSupportedBankList request = new ReqGetSupportedBankList();	
		request.setPayType(type);
		
		RespGetSupportedBankList resp = tradeBankListRpcService.getSupportedBankList(request );
		if (resp != null ) {
			List<RespGetSupportedBankList.BankInfo> dataList = resp.getBankList();
			
			if (dataList != null && !dataList.isEmpty()) {				
				list = new ArrayList<BankInfo>();
				for (RespGetSupportedBankList.BankInfo bankInfo : dataList) {
					if(bankInfo != null){
						BankInfo bInfo = new BankInfo();
						bInfo.setBankId(bankInfo.getBankId());
						bInfo.setBankName(bankInfo.getBankName());
						bInfo.setSmallLogoUrl(bankInfo.getSmallLogoUrl());
						bInfo.setBigLogoUrl(bankInfo.getBigLogoUrl());
						bInfo.setSwiftCode(bankInfo.getSwiftcode());
						bInfo.setCardType(bankInfo.getCardType());
						bInfo.setPassClipherPay(bankInfo.isPassCipherPay());
						list.add(bInfo);
					}
				}
			}
		}
		
		return list;
	}

	@Override
	public int queryBindCardFree(String cardNo) {
		cardNo = YHBFSensitiveEnc.passDatasensitiveEnc(cardNo,"cardno");
		BankCardBindingRpcService bankCardBindingRpcService = dubboConsumerFactory.getDubboClient("bankCardBindingRpcService");	
		
		ReqQueryBindBankCardInfo req = new ReqQueryBindBankCardInfo();
		req.setBankCardNo(cardNo);
		
		RespQueryBindBankCardInfo resp = bankCardBindingRpcService.queryBindBankCardInfo(req );
		if (resp != null) {
			return resp.getMoneyRecharged();
		}
		
		return 0;
	}

}
